//
//  ByViewDetailSkuInfo.m
//  oc_knowledge
//
//  Created by pro on 17/1/18.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByViewDetailSkuInfo.h"

@implementation ByViewDetailSkuInfo
{
    UIView      *_bgView;           // 背景
    UIView      *_line;             // 线条
    UIImageView *_imgView;          // 主图
    UIButton    *_closeBtn;         // 关闭
    UILabel     *_priceLabel;       // 价格
    UILabel     *_storeLabel;       // 库存
    UILabel     *_skuDescLabel;     // 规格描述
    
}
- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor clearColor];
        [self setupUI];
    }
    return self;
}
- (void)setupUI {
    
    _imgView = [[UIImageView alloc] init];
    _imgView.backgroundColor = BY_BLUE_COLOR;
    [self addSubview:_imgView];
    
    _closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _closeBtn.backgroundColor = BY_BLUE_COLOR;
    [_closeBtn setImage:[UIImage imageNamed:@"product_detail_close"] forState:UIControlStateNormal];
    [_closeBtn addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_closeBtn];
    
    _priceLabel = [[UILabel alloc] init];
    _priceLabel.adjustsFontSizeToFitWidth = YES;
    _priceLabel.font = FONT(24);
    _priceLabel.text = @"￥88888.88";
    _priceLabel.textColor = BY_RED_COLOR;
    _priceLabel.textAlignment = NSTextAlignmentLeft;
    [self addSubview:_priceLabel];
    
    _skuDescLabel = [[UILabel alloc] init];
    _skuDescLabel.numberOfLines = 2;
    _skuDescLabel.adjustsFontSizeToFitWidth = YES;
    _skuDescLabel.font = FONT(14);
    _skuDescLabel.text = @"请选择参数";
    _skuDescLabel.textColor = [UIColor lightGrayColor];
    _skuDescLabel.textAlignment = NSTextAlignmentLeft;
    [self addSubview:_skuDescLabel];
    
    [_imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@10);
        make.bottom.equalTo(@0);
        make.height.equalTo(self).multipliedBy(1.33);
        make.width.equalTo(_imgView.mas_height);
    }];
    [_closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@10);
        make.right.equalTo(@-10);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    [_priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.mas_centerY).offset(0);
        make.left.equalTo(_imgView.mas_right).offset(10);
        make.right.lessThanOrEqualTo(_closeBtn.mas_left).offset(-5);
    }];
    [_skuDescLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_priceLabel);
        make.top.equalTo(self.mas_centerY).offset(5);
        make.right.equalTo(@-10);
    }];
}

- (void)setUnSelectedModel:(ByModelProductDetail *)unSelectedModel {
    
    _unSelectedModel = unSelectedModel;
    
    [self setLabelsAttributes:nil];

}
- (void)setSelectedModel:(SkuModel *)selectedModel {
    
    _selectedModel = selectedModel;
    
    [self setLabelsAttributes:_selectedModel];
}


#pragma mark - 点击
- (void)close {
    
    if (self.closeBlock) {
        self.closeBlock();
    }
}

#pragma mark - 辅助
- (void)setLabelsAttributes:(SkuModel *)model {
    
    if (model) {
        [_imgView sd_setImageWithURL:[NSURL URLWithString:model.icon_url] placeholderImage:DEFAULT_IMAGE completed:nil];
        _priceLabel.text = [NSString stringWithFormat:@"￥%.2lf", model.price.floatValue];
        NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:_priceLabel.text];
        [attrStr addAttribute:NSFontAttributeName value:FONT(14) range:[_priceLabel.text rangeOfString:@"￥"]];
        [attrStr addAttribute:NSFontAttributeName value:FONT(14) range:NSMakeRange(_priceLabel.text.length - 2, 2)];
        _priceLabel.attributedText = attrStr;
        _skuDescLabel.text = model.sku_desc;
    } else {
        [_imgView sd_setImageWithURL:[NSURL URLWithString:_unSelectedModel.main_img] placeholderImage:DEFAULT_IMAGE completed:nil];
        
        _priceLabel.text = [NSString stringWithFormat:@"￥%.2lf", _unSelectedModel.price];
        NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:_priceLabel.text];
        [attrStr addAttribute:NSFontAttributeName value:FONT(14) range:[_priceLabel.text rangeOfString:@"￥"]];
        [attrStr addAttribute:NSFontAttributeName value:FONT(14) range:NSMakeRange(_priceLabel.text.length - 2, 2)];
        _priceLabel.attributedText = attrStr;
        _skuDescLabel.text = @"请选择参数";
    }
}

@end
